'use strict';

define(['angular', 'moment', 'angular-ui-router', '../../../common/utils/service', 'readable-range', '../../login/login', '../change-password/change-password', '../pay/pay'],
	function (angular, moment) {
		var module = angular.module('memberRegister', ['ui.router', 'dataService', 'login', 'memberChangePassword', 'memberPay']);
		module.config(['$stateProvider', function ($stateProvider) {
			$stateProvider
				.state("member.register", {
					url: '/register',
					views: {
						"@": {
							controller: 'MemberRegisterCtrl',
							templateUrl: '/mstore/member/register/register.html'
						}
					}
				})
		}]);
		module.controller('MemberRegisterCtrl', ['$rootScope', '$state', '$scope', 'dataService', '$mdDialog', '$interval', function ($rootScope, $state, $scope, dataService, $mdDialog, $interval) {
			$rootScope.pageTitle = "会员注册";
			var storeId = sessionStorage.store;
			$scope.member = {};
			Promise.promisify(dataService.getStoreConfig)(storeId, 'member.defaultRegisterMemberType').then(function (data) {
				if (!data) {
					throw data;
				}
				if (data.value == 'null') {
					throw data;
				}
				$scope.member.membertype_id = data.value;
			}).catch(function (err) {
				$rootScope.showMessage("店铺还没有开通顾客会员注册，请联系商家开通会员注册");
				$state.transitionTo("home");
			});

			$scope.$watch("member.mobile", function (mobile) {
				if (mobile && mobile.length == 11) {
					$scope.sendCode(mobile);
				}
			})

			$scope.sendCode = function (mobile) {
				var againStr = '再次发送动态登录密码',
					resendInterval = 60,
					cooldown = false;
				var account = {account: mobile};
				if (!/^((13[0-9])|(15[^4,\D])|(18[0-9])|(14[0-9])|(17[0,6-8]))\d{8}$/.test(mobile)) {
					return $rootScope.showMessage('号码格式有误，请输入正确的号码');
				}

				if (cooldown) {
					return;
				}

				cooldown = true;

				dataService.sendMemberRegisterSmsCode(storeId, account, function (err) {
					if (err) {
						return $rootScope.showMessage(err.msg.error);
					}
					$rootScope.showMessage('会员注册码已发送手机"' + mobile + '"');
					$scope.smsBlocked = true;
					$scope.againTime = resendInterval;
					$scope.isSendCode = true;
					var intervalHandler = $interval(function () {
						$scope.againTime--;
						if ($scope.againTime == 0) {
							$scope.smsBlocked = false;
							$interval.cancel(intervalHandler);
							cooldown = false;
						}
					}, 1000);
				});
			};

			$scope.register = function (memberInfo) {
				var member = angular.copy(memberInfo);
				member.storeId = storeId;
				if (!member.name)
					return $rootScope.showMessage("会员名必须填写");
				if (!member.mobile)
					return $rootScope.showMessage("手机号必须填写");
				if (!/^((13[0-9])|(15[^4,\D])|(18[0-9])|(14[0-9])|(17[0,6-8]))\d{8}$/.test(member.mobile)) {
					return $rootScope.showMessage('手机号格式有误，请输入正确的号码');
				}
				if (!member.autoCode)
					return $rootScope.showMessage("验证码必须填写");
				if (member.birth && typeof member.birth == 'object')
					member.birth = moment(member.birth.getTime()).format('YYYY-MM-DD');
				dataService.registerMember(storeId, member, function (err, data) {
					if (err) {
						if (err.msg && typeof err.msg == 'object')
							$rootScope.showMessage(err.msg.error);
						else
							$rootScope.showMessage(err);
						return;
					}
					dataService.getOpenId(sessionStorage.store, sessionStorage.code, sessionStorage.sns, function (err, ret) {
						if (err) return;
						sessionStorage.openId = ret.openid;
						sessionStorage.memberId = data.id;
						delete sessionStorage.code;
						$scope.handleMember();
						$state.transitionTo('member');
					});


				})

			}
		}]);

	});
